﻿using System;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using Habanero.Base;
using Habanero.Testability;

namespace FireStarterModeller.Test.BO
{
    public static class TestUtilsDMSolution
    {
        private readonly static BOTestFactory<DMSolution> _factory = GetTestFactory();

        public static BOTestFactory<DMSolution> GetTestFactory()
        {
            return BOTestFactoryRegistry.Instance.Resolve<DMSolution>();
        }
        /// <summary>
        /// Creates a new saved DMSolution with a random value assigned to every property
        /// </summary>
        public static DMSolution CreateSavedDMSolution()
        {
            DMSolution dMSolution = CreateUnsavedValidDMSolution();
            dMSolution.Save();
            return dMSolution;
        }

        /// <summary>
        /// Creates a new unsaved DMSolution with a random value assigned to every property
        /// </summary>
        public static DMSolution CreateUnsavedValidDMSolution()
        {
            SolutionCreator solutionCreator = new SolutionCreator();
            DMSolution dMSolution = solutionCreator.CreateSolution();
            UpdateWithValidData(dMSolution);
            return dMSolution;
        }

        public static void UpdateWithValidData(DMSolution dMSolution)
        {
            dMSolution.SolutionName = TestUtilsShared.GetRandomString(2,100);
            dMSolution.DefaultAssembly = TestUtilsDMAssembly.CreateSavedDMAssembly();
            dMSolution.HabaneroDllPath = TestUtilsShared.GetRandomString();
            dMSolution.TableNamePrefix = TestUtilsShared.GetRandomString();
            dMSolution.TableNameSuffix = TestUtilsShared.GetRandomString();
            dMSolution.PascalCase = TestUtilsShared.GetRandomBoolean();
            dMSolution.SolutionDirectory = TestUtilsShared.GetRandomString();
            dMSolution.UIProjectName = TestUtilsShared.GetRandomString();
            dMSolution.BOProjectName = TestUtilsShared.GetRandomString();
            dMSolution.LogicProjectName = TestUtilsShared.GetRandomString();
            dMSolution.BaseProjectName = TestUtilsShared.GetRandomString();
            dMSolution.Generated = false;
            dMSolution.BOTestProjectName = TestUtilsShared.GetRandomString();
            dMSolution.UITestProjectName = TestUtilsShared.GetRandomString();
        }

        /// <summary>
        /// Creates a new unsaved DMSolution where all properties are null, except ID properties
        /// and those with default values.  If there are compulsory properties without
        /// defaults, saving the object will throw an exception.
        /// </summary>
        public static DMSolution CreateUnsavedDefaultDMSolution()
        {
            DMSolution dMSolution = new DMSolution();
            return dMSolution;
        }

        public static DMSolution CreateSavedDMSolutionWithTwoClasses()
        {
            DMSolution solution = CreateSavedDMSolution();
            solution.DefaultAssembly = TestUtilsDMAssembly.CreateSavedDMAssembly();
            TestUtilsDMClass.CreateUnsavedDefaultDMClass(solution);
            TestUtilsDMClass.CreateUnsavedDefaultDMClass(solution);
            return solution;
        }


    }
// ReSharper disable UnusedMember.Global
    //This is used by the BOTestRegistry to autoregister this solution Factory.
    public class BOTestFactorySolution : BOTestFactory<DMSolution>
    {
        public BOTestFactorySolution(DMSolution solution)
            : base(solution)
        {
        }

        public BOTestFactorySolution()
        {
        }
        public override DMSolution CreateDefaultBusinessObject()
        {
            SolutionCreator solutionCreator = new SolutionCreator();
            return solutionCreator.CreateSolution();
        }
    }
    // ReSharper restore UnusedMember.Global

}